package com.filter;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebFilter(filterName = "CORSFilter", urlPatterns = {"/*"})
////@Order(value=1)
public class CORSFilter implements Filter{


        @Override
        public void init(FilterConfig filterConfig) throws ServletException {

        }

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            System.out.println("work");
//            HttpServletResponse response = (HttpServletResponse) servletResponse;
//            response.setHeader("Access-Control-Allow-Origin", "*");
//            response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
//            response.setHeader("Access-Control-Max-Age", "3600");
//            response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
//            response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
//            response.addHeader("Access-Control-Allow-Origin", "*");
//            response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
//            response.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
//            response.addHeader("Access-Control-Max-Age", "3600");

//            String origin = (String) servletRequest.getRemoteHost()+":"+servletRequest.getRemotePort();
//            response.setHeader("Access-Control-Allow-Origin", "*");
//            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
//            response.setHeader("Access-Control-Max-Age", "3600");
//            response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
//            response.setHeader("Access-Control-Allow-Credentials","true");
//            filterChain.doFilter(servletRequest, servletResponse);


            HttpServletRequest request = (HttpServletRequest) servletRequest;
            HttpServletResponse response = (HttpServletResponse) servletResponse;

            // 响应标头指定 指定可以访问资源的URI路径
            response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

            //响应标头指定响应访问所述资源到时允许的一种或多种方法
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");

            //设置 缓存可以生存的最大秒数
            response.setHeader("Access-Control-Max-Age", "3600");

            //设置  受支持请求标头
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with");

            // 指示的请求的响应是否可以暴露于该页面。当true值返回时它可以被暴露
            response.setHeader("Access-Control-Allow-Credentials","true");

//            filterChain.doFilter(servletRequest, servletResponse);






            System.out.println("结束过滤");

            filterChain.doFilter(servletRequest, servletResponse);
        }

        @Override
        public void destroy() {

        }
}

